//
//  HLRtcRechargeView.m
//  YueYue
//
//  Created by hulu001 on 2023/8/25.
//

#import "HLRtcRechargeView.h"
#import <WebKit/WebKit.h>

@interface HLRtcRechargeView ()
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) QMUIButton *closeButton;
@property (nonatomic, strong) WKWebView *webView;

@end

@implementation HLRtcRechargeView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI
{
    self.backgroundColor = UIColorWhite;
    self.layer.masksToBounds = YES;
    self.layer.cornerRadius = 20;
    self.titleLabel = [[UILabel alloc] qmui_initWithFont:UIFontMake(18) textColor:HLTextColor1];
    _titleLabel.text = @"充值中心";
    [self addSubview:_titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(16);
    }];
    
    self.closeButton = [[QMUIButton alloc] init];
    [_closeButton setImage:UIImageMake(@"rtc_result_close") forState:UIControlStateNormal];
    [self addSubview:_closeButton];
    [_closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-12);
        make.size.mas_equalTo(32);
        make.centerY.equalTo(_titleLabel);
    }];
    

    [self addSubview:self.webView];
    [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.equalTo(_titleLabel.mas_bottom).offset(16);
    }];
}


- (WKWebView *)webView{
    if(_webView == nil){
        
        //创建网页配置对象
        WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
        
        // 创建设置对象
        WKPreferences *preference = [[WKPreferences alloc]init];
        //最小字体大小 当将javaScriptEnabled属性设置为NO时，可以看到明显的效果
        preference.minimumFontSize = 0;
        //设置是否支持javaScript 默认是支持的
        preference.javaScriptEnabled = YES;
        // 在iOS上默认为NO，表示是否允许不经过用户交互由javaScript自动打开窗口
        preference.javaScriptCanOpenWindowsAutomatically = YES;
        config.preferences = preference;
        
        // 是使用h5的视频播放器在线播放, 还是使用原生播放器全屏播放
        config.allowsInlineMediaPlayback = YES;
        //设置视频是否需要用户手动播放  设置为NO则会允许自动播放
        config.mediaTypesRequiringUserActionForPlayback = YES;
        //设置是否允许画中画技术 在特定设备上有效
        config.allowsPictureInPictureMediaPlayback = YES;
        //设置请求的User-Agent信息中应用程序名称 iOS9后可用
        config.applicationNameForUserAgent = ClientID;
        
        //这个类主要用来做native与JavaScript的交互管理
        WKUserContentController * wkUController = [[WKUserContentController alloc] init];
        config.userContentController = wkUController;
        
        _webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:config];
        [_webView.scrollView  setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
        _webView.scrollView.showsVerticalScrollIndicator = NO;
        // 是否允许手势左滑返回上一级, 类似导航控制的左滑返回
        _webView.allowsBackForwardNavigationGestures = YES;
        //可返回的页面列表, 存储已打开过的网页
//        WKBackForwardList * backForwardList = [_webView backForwardList];
        
    }
    return _webView;
}

- (void)loadWithUrl:(NSString *)url
{
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
    [self.webView loadRequest:request];
    
}


+ (void)show
{
    CGFloat height = SCREEN_HEIGHT - 108;

    HLRtcRechargeView *contentView = [[HLRtcRechargeView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, height)];
    [contentView loadWithUrl:[HLHelper addTokenParamWithBaesURL:HLH5IncomeUrl]];
    QMUIModalPresentationViewController *modalViewController = [[QMUIModalPresentationViewController alloc] init];
    modalViewController.contentViewMargins = UIEdgeInsetsMake(SCREEN_HEIGHT-height, 0, 0, 0);
    modalViewController.contentView = contentView;
    modalViewController.dimmingView.backgroundColor = [UIColorBlack colorWithAlphaComponent:0.3];
    [modalViewController showWithAnimated:YES completion:nil];
    
    [contentView.closeButton setQmui_tapBlock:^(__kindof UIControl *sender) {
        [modalViewController hideWithAnimated:NO completion:nil];
    }];
}

@end
